package com.xmy.cultivate.mapper;

import com.xmy.cultivate.entity.AuthAdminContent;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xmy.cultivate.entity.response.AllotDataRs;
import com.xmy.cultivate.entity.views.LeadDataRs;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 内容【数据】权限 Mapper 接口
 * </p>
 *
 * @author hpiggy
 * @since 2025-04-26
 */
public interface AuthAdminContentMapper extends BaseMapper<AuthAdminContent> {

    @Select("select GROUP_CONCAT(DISTINCT school_id) as school_ids,GROUP_CONCAT(DISTINCT year_class_id) as year_class_ids,GROUP_CONCAT(DISTINCT subjects_id) as subjects_ids from grade where year_part = ${yearPart} and quarter_num = ${quarterNum} and lesson_type =1 and teacher_id = ${teacherId} and deleted = 0 GROUP BY teacher_id")
    public AuthAdminContent getTeacherData(@Param("yearPart") Integer yearPart,@Param("quarterNum") Integer quarterNum,@Param("teacherId") Long teacherId);

    @Select("select GROUP_CONCAT(DISTINCT school_id) as school_ids,GROUP_CONCAT(DISTINCT year_class_id) as year_class_ids,GROUP_CONCAT(DISTINCT subjects_id) as subjects_ids from grade where year_part = ${yearPart} and quarter_num = ${quarterNum} and lesson_type =1 and teacher_id = ${teacherId} and deleted = 0 GROUP BY teacher_id,subjects_id")
    public LeadDataRs getLeadData(@Param("yearPart") Integer yearPart, @Param("quarterNum") Integer quarterNum, @Param("teacherId") Long teacherId);

    @Select({"<script>",
            "SELECT",
                    "GROUP_CONCAT(DISTINCT t3.school_ids) as school_ids,",
                    "GROUP_CONCAT(DISTINCT t3.year_class_ids) as year_class_ids,",
                    "GROUP_CONCAT(DISTINCT t3.subjects_ids) as subjects_ids",
                    "FROM",
                    "auth_allot_teacher AS t1",
                    "INNER JOIN auth_allot_teacher_projects AS t2 ON t1.id = t2.auth_teacher_id",
                    "INNER JOIN auth_allot_teacher_school AS t3 ON t1.id = t3.auth_teacher_id ",
                    "INNER JOIN mobile_permission as t4 on t2.projects_id = t4.id ",
                    "WHERE",
                    "#{nowTime} BETWEEN start_date  AND end_date ",
                    "and t1.teacher_id = ${teacherId}",
                    "and t4.`code` in(${projectName})",
                    "and t1.deleted = 0",
            "</script>"})
    public List<AllotDataRs> getAllotData(@Param("teacherId") Long teacherId,
                                          @Param("weeksIndex") Integer weeksIndex,
                                          @Param("nowTime") String nowTime,
                                          @Param("projectName") String projectName);

    @Select({"<script>",
            "SELECT",
            "t4.code",
            "FROM",
            "auth_allot_teacher AS t1",
            "INNER JOIN auth_allot_teacher_projects AS t2 ON t1.id = t2.auth_teacher_id",
            "INNER JOIN auth_allot_teacher_school AS t3 ON t1.id = t3.auth_teacher_id ",
            "INNER JOIN mobile_permission as t4 on t2.projects_id = t4.id ",
            "WHERE",
            "((",
            "t1.type = 1 ",
            "AND #{nowTime} BETWEEN start_date ",
            "AND end_date ",
            ") ",
            "OR ( t1.type = 2 AND FIND_IN_SET( ${weeksIndex}, weeks ) AND #{nowTime} BETWEEN start_date AND end_date )) ",
            "and t1.teacher_id = ${teacherId}",
            "and t1.deleted = 0",
            "</script>"})
    public List<String> getAllotCode(@Param("teacherId") Long teacherId,
                                          @Param("weeksIndex") Integer weeksIndex,
                                          @Param("nowTime") String nowTime);

}
